<!DOCTYPE html>
<html class="x-admin-sm" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.2</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="stylesheet" th:href="@{/xadmin/css/font.css}">
    <link rel="stylesheet" th:href="@{/xadmin/css/xadmin.css}">
    <script th:src="@{/xadmin/lib/layui/layui.js}" charset="utf-8"></script>
    <script type="text/javascript" th:src="@{/xadmin/js/xadmin.js}"></script>
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style>
        .layui-table-body {
            overflow-x: hidden;
        }
    </style>
</head>

<body>
<div class="x-nav">
            <span class="layui-breadcrumb">
                <a href="">首页</a>
                <a href="">系统管理</a>
                <a>
                    <cite>年级列表</cite></a>
            </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
    </a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body hide_form" style="display: none;">
                    <form class="layui-form layui-col-space5">
                        <!--                        <label class="layui-form-label">添加系部:</label>-->
                        <div class="layui-inline layui-show-xs-block">
                            <input class="layui-input" required autocomplete="off" placeholder="年级名" name="name"
                                   type="number"
                                   min="2000"
                                   max="2999"
                                   lay-verify="gradename">
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <button class="layui-btn" lay-submit="" lay-filter="add">
                                <i class="layui-icon">添加</i></button>
                        </div>
                    </form>
                </div>
                <div class="layui-card-body">
                    <table class="layui-table"
                           lay-data="{url:'/grade',page:false,toolbar: '#toolbarDemo',id:'test',initSort:{field:'id', type:'asc'}}"
                           lay-filter="test">
                        <thead>
                        <tr>
                            <th lay-data="{type:'checkbox'}">ID</th>
                            <th lay-data="{type: 'numbers', width:80, sort: true}">ID</th>
                            <th lay-data="{field:'name', minWidth:100, sort: true, edit: 'text'}">年级名</th>
                        </tr>
                        </thead>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <div class="layui-inline" title="批量删除" lay-event="delete"><i class="layui-icon layui-icon-delete"></i></div>
        <div class="layui-inline" title="添加" lay-event="add"><i class="layui-icon layui-icon-add-1"></i></div>
    </div>
</script>
<script>
    layui.use(['form', 'table'], function () {
        var form = layui.form;
        var table = layui.table;
        //监听单元格编辑
        table.on('edit(test)', function (obj) {
            var value = obj.value //得到修改后的值
                , data = obj.data //得到所在行所有键值
                , field = obj.field; //得到字段
            // layer.msg('[ID: ' + data.id + '] ' + field + ' 字段更改为：' + value);
            if (!/^\d+$/.test(value)) {
                return layer.alert("内容必须为数字", {icon: 2}, function (index1) {
                    layer.close(index1);
                    table.reload('test');
                });
            }
            var load = layer.load(2)
            $.ajax({
                url: "/grade/edit",
                type: "POST",
                data: {
                    id: data.id,
                    name: value
                },
                dataType: 'json',
                success: function (result) {
                    layer.close(load)
                    if (result.code === 500 || result.code === 5000100 || result.code === 5000101 || result.code === 5000102) {
                        layer.alert(result.msg, {icon: 2}, function (index1) {
                            layer.close(index1);
                            table.reload('test');
                        });
                    } else {
                        layer.alert("修改成功", {icon: 1}, function (index1) {
                            layer.close(index1);
                            table.reload('test');
                        });
                    }
                }
            });
        });

        //头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'delete':
                    var data = checkStatus.data;
                    var idArray = [];
                    for (var i in data) {
                        idArray[i] = data[i].id;
                    }
                    if (idArray.length === 0) {
                        layer.alert("未选中", {icon: 5});
                        break;
                    }
                    console.log(idArray.toString());
                    layer.confirm('真的要删除? 请谨慎操作!', function (index) {
                        var load = layer.load(2)
                        $.ajax({
                            url: '/grade',
                            type: "DELETE",
                            data: {ids: idArray},
                            dataType: 'json',
                            success: function (result) {
                                layer.close(load)
                                if (result.code === 500 || result.code === 5000100 || result.code === 5000101 || result.code === 5000102) {
                                    layer.alert(result.msg, {icon: 2});
                                } else {
                                    layer.alert("删除成功", {icon: 1}, function (index1) {
                                        layer.close(index1);
                                        //xadmin.father_reload();
                                        table.reload('test');
                                    });
                                }
                            },
                        });
                    });
                    // layer.alert(JSON.stringify(data));
                    break;
                case 'add':
                    let hide_add = $('.hide_form');
                    if (hide_add.css('display') === "none") {
                        hide_add.show();
                    } else {
                        hide_add.hide();
                    }
                    // $('.hide_form').show();
                    // xadmin.open('用户添加', '/getPage?name=dept/dept-add');
                    break;
            }
        });
        form.on('submit(add)', function (data) {
            console.log(data.field);
            var load = layer.load(2)
            $.ajax({
                url: "/grade/add",
                type: "POST",
                data: {name: data.field['name']},
                dataType: 'json',
                success: function (result) {
                    layer.close(load)
                    if (result.code === 500 || result.code === 5000100 || result.code === 5000101 || result.code === 5000102) {
                        layer.alert(result.msg, {icon: 2});
                    } else {
                        layer.alert("添加成功", {icon: 6}, function () {
                            location.reload();
                        });
                    }
                }
            });
            return false;
        });

        form.verify({
            gradename: function (value) {
                if (value == null || value === '') {
                    return '年级不能为空'
                }
                if (!/^\d+$/.test(value)) {
                    return "内容必须为数字";
                }
            },
        });
    });
</script>
</html>